package indexer;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.DoubleField;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import databaseconnector.DBProcessor;

/*
 * 英文租房信息建立器
 */
public class RentEn_index {

		//MySQL数据库连接器
		private static DBProcessor processor;
		
		//构造函数
		public RentEn_index()
		{
			processor = new DBProcessor();
		}

		//从Rent数据库抽取英文数据建立索引
		public static void buildRentEnIndex(boolean create) throws IOException {
			
			//索引输出目录
			Directory dir = FSDirectory.open(new File("E:/Index/RentEnIndex"));
			
			IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_40,
					new SmartChineseAnalyzer(Version.LUCENE_40));
			
			if (create) {
				cfg.setOpenMode(OpenMode.CREATE);
			} else {
				cfg.setOpenMode(OpenMode.CREATE_OR_APPEND);
			}
			
			IndexWriter writer = new IndexWriter(dir, cfg);
			ResultSet rlt = processor.getRent();
			Document doc = new Document();
			
			//Field域构建
			Field id = new StoredField("id", Integer.MIN_VALUE);
			
			//**pw
			Field price = new DoubleField("price", 0, Field.Store.YES);
			
			//numbers of beds:*
			Field house = new IntField("house", 0,Field.Store.YES);
			
			Field decoration = new TextField("decoration", "",Field.Store.YES);
			Field area = new IntField("area", Integer.MIN_VALUE, Field.Store.YES);
			Field year = new StoredField("year", "");
			
			Field stair = new TextField("stair", "", Field.Store.YES);
			
			//property type:Flat
			Field build = new TextField("build", "", Field.Store.YES);
			
			//存放seller type Trade(不知道什么意思，不用管)
			Field direction = new TextField("direction", "", Field.Store.YES);
			
			Field region = new TextField("region", "", Field.Store.YES);
			
			//person、phone
			Field commlink = new StoredField("commlink","");
			Field imglink = new StoredField("imglink", "");
			
			
			
			//将Field域加入到doc中
			doc.add(id);
			doc.add(price);
			doc.add(house);
			doc.add(decoration);
			doc.add(area);
			doc.add(year);
			doc.add(stair);
			doc.add(build);
			doc.add(direction);
			doc.add(region);
			doc.add(commlink);
			doc.add(imglink);
			
			
			//为doc的各Field赋值
			String strPrice,strArea,strYear;
			String tmpStr;

			try {
				while (rlt.next()) {
					
					//Id超过41595后为英文租房信息，需要进行其他处理
					if(rlt.getInt(1) <= 41595){
						continue;
					}
					
					
					//设置id值
					id.setIntValue(rlt.getInt(1));
					System.out.print(rlt.getInt(1) +  " ");
					
					
					//设置price值
					strPrice = rlt.getString(2);
					if(strPrice.contains("pw") || strPrice.contains("pm"))
					{
						strPrice = strPrice.replaceAll(",", "").trim();
						price.setDoubleValue(Double.parseDouble(strPrice.substring(0,
							strPrice.length() - 2)));
						//System.out.print(Double.parseDouble(strPrice.substring(0,
								//strPrice.length() - 2)) + " ");
					}
					
					//设置house值
					String tempHouse = rlt.getString(3);
					if(tempHouse.contains("Number of beds:"))
					{
						tempHouse = tempHouse.replaceAll("Number of beds:","").trim();
						try
						{
							int bed_num = Integer.parseInt(tempHouse);
							house.setIntValue(bed_num);
							//System.out.print(bed_num + " ");
						}
						catch(Exception e){}
					}
					
					//设置decoration值
					tmpStr = rlt.getString(4);
					if(tmpStr != null){
						decoration.setStringValue(tmpStr);
						//System.out.print(tmpStr + " ");
					}
					
					//设置area值
					strArea = rlt.getString(5);
					if(strArea != null)
					{
						area.setIntValue(Integer.parseInt(strArea));
						//System.out.print(strArea + " ");
					}
					
					//设置year值
					strYear = rlt.getString(6);
					if(strYear != null)
					{
						year.setStringValue(strYear);
						//System.out.print(strYear + " ");
					}
					
					//设置stair值
					tmpStr = rlt.getString(7);
					if(tmpStr != null)
					{
						stair.setStringValue(tmpStr);
						//System.out.print(tmpStr + " ");
					}
					
					
					//设置build值
					tmpStr = rlt.getString(8);
					if(tmpStr != null){
						tmpStr = tmpStr.replaceAll("Property type:", "").trim();
						build.setStringValue(tmpStr);
						//System.out.print(tmpStr + " ");
					}
					
					//设置direction值
					tmpStr = rlt.getString(9);
					if(tmpStr != null){
						direction.setStringValue(tmpStr);
						//System.out.print(tmpStr + " ");
					}
					
					//设置region值
					tmpStr =rlt.getString(10);
					if(tmpStr != null){
						region.setStringValue(tmpStr);
						System.out.print(tmpStr + " ");
					}
					
					//设置commlink值
					tmpStr = rlt.getString(11);
					if(tmpStr != null){
						commlink.setStringValue(tmpStr);
						//System.out.print(tmpStr + " ");
					}
					
					//设置imglink值
					tmpStr = rlt.getString(12);
					if(tmpStr != null){
						imglink.setStringValue(tmpStr);
						//System.out.print(tmpStr + " ");
					}
					
					System.out.println();
					writer.addDocument(doc);

				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (create) {
				writer.forceMerge(1);
			}
			writer.close();
		}
}
